package com.tiza.xiaoyoubao.ui.fragment;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.squareup.picasso.Picasso;
import com.tianze.library.adapter.recycler.RecyclerBaseAdapter;
import com.tianze.library.base.BaseStoreHouseRefreshRecyclerFragment;
import com.tianze.library.view.EmptyLayout;
import com.tiza.xiaoyoubao.R;
import com.tiza.xiaoyoubao.app.Constants;
import com.tiza.xiaoyoubao.app.UIHelper;
import com.tiza.xiaoyoubao.entity.TbTaskOperateInfo;
import com.tiza.xiaoyoubao.entity.UserInfo;
import com.tiza.xiaoyoubao.net.ApiHttpClient;
import com.tiza.xiaoyoubao.net.JsonCallback;
import com.tiza.xiaoyoubao.net.ResponseInfo;
import com.tiza.xiaoyoubao.utils.DateUtils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * Created by mapple on 17/3/1.
 * 特别任务未完成待操作
 */

public class TbOperateFragment extends BaseStoreHouseRefreshRecyclerFragment {

    UserInfo userInfo = Hawk.get(Constants.KEY_USER_INFO, (UserInfo) null);

    private List<TbTaskOperateInfo> infoList;
    DateFormat df;
    Date date1;
    Date date2;
    private String cid, taskTime, orderid, buyerid, taskCode, reason, account, sellerID;

    @Override
    protected RecyclerBaseAdapter getListAdapter() {
        return new TaskOperateAdapter();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pageSize = Integer.MAX_VALUE;
        df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    }

    @Override
    protected void onItemClick(View view, int position) {
        super.onItemClick(view, position);
        TbTaskOperateInfo info = (TbTaskOperateInfo) mAdapter.getData().get(position);
        Bundle args = new Bundle();
        args.putSerializable("info", info);
        args.putString("state", "operate");
        UIHelper.showDianFuTbTask2(mActivity, args);
        finish();
    }

    @Override
    protected void requestData(boolean refresh) {
        super.requestData(refresh);
        ApiHttpClient.receivedTbTask(this, userInfo.getWebzzCmsBuyerInfo().getUserid(), "001", "1",
                                     new JsonCallback<TbTaskOperateInfo>(TbTaskOperateInfo.class) {

                                         @Override
                                         public void onError(Call call, Exception e) {
                                             onRequestError();
                                         }

                                         @Override
                                         public void onResponse(
                                                 ResponseInfo<TbTaskOperateInfo> response
                                         ) {
                                             int code = response.getCode();
                                             if (code == 1) {
                                                 mErrorLayout.setErrorType(
                                                         EmptyLayout.NETWORK_ERROR);
                                                 return;
                                             }
                                             infoList = response.getData();
                                             onRequestResponse(infoList);
                                         }

                                         @Override
                                         public void onAfter() {
                                             super.onAfter();
                                             onRequestFinish();
                                         }
                                     }
        );
    }

    public class TaskOperateAdapter extends RecyclerBaseAdapter {

        @Override
        protected View onCreateItemView(ViewGroup parent, int viewType) {
            return getLayoutInflater(parent.getContext()).inflate(R.layout.item_operate_fragment,
                                                                  parent, false
            );
        }

        @Override
        protected TaskOperateAdapter.ViewHolder onCreateItemViewHolder(
                View view, int viewType
        ) {
            return new TaskOperateAdapter.ViewHolder(viewType, view);
        }

        @Override
        public void onBindItemViewHolder(RecyclerBaseAdapter.ViewHolder holder, int position) {
            super.onBindItemViewHolder(holder, position);
            final ViewHolder vh = (ViewHolder) holder;
            TbTaskOperateInfo info = (TbTaskOperateInfo) _data.get(position);
            vh.tv_userWangWang_Name.setText(info.getShopName());
            vh.tv_time.setText(DateUtils.getDateToStringDay(info.getAddTime()));
            vh.tv_money.setText(DateUtils.formateRate(String.valueOf(info.getBaseCmi())));
            vh.tv1.setText("平台垫资"+DateUtils.formateRate(String.valueOf(info.getPnumber())));
            vh.tv2.setText(info.getBackName());
            Picasso.with(mActivity).load(ApiHttpClient.URL2 + info.getImages()).placeholder(
                    R.mipmap.rwcx_jiazaizhong).error(R.mipmap.rwcx_jiazaizhong).into(vh.iv_goods);
        }

        class ViewHolder extends RecyclerBaseAdapter.ViewHolder {

            @BindView(R.id.tv_userWangWang_Name)
            TextView tv_userWangWang_Name;
            @BindView(R.id.tv_time)
            TextView tv_time;
            @BindView(R.id.tv_money)
            TextView tv_money;
            @BindView(R.id.tv1)
            TextView tv1;
            @BindView(R.id.tv2)
            TextView tv2;
            @BindView(R.id.iv_goods)
            ImageView iv_goods;
            @BindView(R.id.iv_userWangWang)
            ImageView iv_userWangWang;

            ViewHolder(int viewType, View view) {
                super(viewType, view);
                ButterKnife.bind(this, view);
            }
        }

    }
}
